﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsClient
{
    public partial class mainForm : Form
    {
        readonly string appName = "gps tethering client";
        UDPMulticastConnector udpConnector { get; set; }
        private const string GROUP_ADDRESS = "224.0.1.11";
        private const int GROUP_PORT = 1125;
        private const string COM_NAME = "COM9";
        private const int COM_BAUD = 19200;
        SerialPort serialPort;


        public mainForm()
        {
            InitializeComponent();
            txtDataReceice.ReadOnly = true;
        }

        private void mainForm_Load(object sender, EventArgs e)
        {
            //this.ShowInTaskbar = false;
            //this.Hide();
            startToolStripMenuItem_Click(this, new EventArgs());
        }

        private void hideToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowInTaskbar = false;
            this.Hide();
        }

        private void showToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.ShowInTaskbar = true;
            this.Show();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void startToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Text = appName + " working";
            if (null == udpConnector)
            {
                serialPort = new SerialPort();
                serialPort.PortName = COM_NAME;
                serialPort.BaudRate = COM_BAUD;
                serialPort.StopBits = StopBits.One;
                serialPort.DataBits = 8;
                serialPort.Open();
                udpConnector = new UDPMulticastConnector(GROUP_ADDRESS, GROUP_PORT);
                udpConnector.PacketReceived += new EventHandler<UdpPacketReceivedEventArgs>(udpConnector_PacketReceived);
                udpConnector.StartMulticast();
            }
        }

        void udpConnector_PacketReceived(object sender, UdpPacketReceivedEventArgs e)
        {
            string str = string.Format("{0}\r\n", e.Message);
            UpdateText(txtDataReceice, str);
            byte[] bytes = Encoding.UTF8.GetBytes(e.Message);
            serialPort.Write(bytes, 0, bytes.Length);
        }

        private delegate void StDelegate(RichTextBox txt, string value);
        private void UpdateText(RichTextBox txt, string value)
        {
            if (txt.InvokeRequired)
            {
                StDelegate DDD = new StDelegate(UpdateText);
                txt.Invoke(DDD, new object[] { txt, value });
            }
            else
            {
                txt.AppendText(value);
                //txt.AppendText("\n");
                txt.ScrollToCaret();
            }
        }

        private void stopToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Text = appName + " stopped";
            if (null != udpConnector)
            {
                udpConnector.Stop();
                udpConnector = null;
                serialPort.Close();
                serialPort = null;
            }

        }
    }
}
